package com.william.yun.yunliwuli.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public abstract class AbstractHttpUtils {

    public  abstract String get(String uri, Map<String, String> headers) throws IOException;

    public abstract String post(String uri, JSONObject object,  Map<String, String> headers) throws IOException;

    public abstract String post(String uri, JSONArray array, Map<String, String> headers) throws IOException;

    public abstract String put(String url, JSONObject object, HashMap<String, String> headers) throws IOException;

    public abstract String delete(String url, JSONObject body,  HashMap<String, String> headers) throws IOException;

    public String getHttp(String url, Map<String, String> headers) throws IOException {
        log.info("url:{}", url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        headers.forEach(httpGet::addHeader);
        return execute(httpGet, httpClient);

    }

    public String postHttp(String url, JSONObject body,  Map<String, String> headers) throws IOException {
        log.info("url:{}", url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        String header = headers.get(HttpHeaders.CONTENT_TYPE);
        if(StringUtils.isNotEmpty(header) && header.contains("json")){
            StringEntity entity = new StringEntity(body.toJSONString(), StandardCharsets.UTF_8);
            httpPost.setEntity(entity);
        }
        headers.forEach(httpPost::addHeader);
        return execute(httpPost, httpClient);
    }

    public String postHttp(String url, JSONArray array,  Map<String, String> headers) throws IOException {
        log.info("url:{}", url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        String header = headers.get(HttpHeaders.CONTENT_TYPE);
        if(StringUtils.isNotEmpty(header) && header.contains("json")){
            StringEntity entity = new StringEntity(array.toJSONString(), StandardCharsets.UTF_8);
            httpPost.setEntity(entity);
        }
        headers.forEach(httpPost::addHeader);
        return execute(httpPost, httpClient);
    }

    public String putHttp(String url, JSONObject body,  Map<String, String> headers) throws IOException {
        log.info("url:{}", url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut(url);
        String header = headers.get(HttpHeaders.CONTENT_TYPE);
        if(StringUtils.isNotEmpty(header) && header.contains("json")){
            StringEntity entity = new StringEntity(body.toJSONString(), StandardCharsets.UTF_8);
            httpPut.setEntity(entity);
        }
        headers.forEach(httpPut::addHeader);
        return execute(httpPut, httpClient);
    }

    public String deleteHttp(String url, JSONObject body,  Map<String, String> headers) throws IOException {
        log.info("url:{}", url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
        log.info("httpDelete:{}", httpDelete.toString());
        String header = headers.get(HttpHeaders.CONTENT_TYPE);
        if(StringUtils.isNotEmpty(header) && header.contains("json")){
            StringEntity entity = new StringEntity(body.toJSONString(), StandardCharsets.UTF_8);
            httpDelete.setEntity(entity);
        }
        headers.forEach(httpDelete::addHeader);
        return execute(httpDelete, httpClient);
    }


    private String execute(HttpRequestBase httpRequestBase, CloseableHttpClient httpClient) throws IOException {
        CloseableHttpResponse execute = httpClient.execute(httpRequestBase);
        HttpEntity entity = execute.getEntity();
        String result =  EntityUtils.toString(entity);
        log.info("result:{}", result);
        return result;
    }
}
